package com.lz.football.module.ladderMatch
{
	import com.common.utils.Reflection;
	import com.liangzhong.model.AppData;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.ladderMatch.item.LadderLvTabMenu;
	import com.lz.football.module.ladderMatch.item.RankingItem;
	
	import common.BaseView.BaseView;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;


	/**
	 * 天梯排名  
	 * @author Administrator
	 * 
	 */	
	public class RankingPanel extends BaseView implements IComponent
	{
		/**
		 *显示长度 
		 */
		private const ITEMLENGTH:int = 10;
//		private var symbolSkin		:MovieClip;//皮肤
//		/**
//		 *关闭按钮  
//		 */
//		private var closeBtn:DisplayObject;
		/**
		 *item容器 
		 */
		private var _contain:MovieClip;
//		/**
//		 *换页  
//		 */
//		private var _page:PagePanel;
		/**
		 * 
		 */
		private var _menu:LadderLvTabMenu;
		/**
		 * 
		 */
		private var _itemVec:Vector.<RankingItem>;
//		/**
//		 *  
//		 */
//		private var _ladderInfo:LadderBaseInfo;
		/**
		 * 当前排名 
		 */
		private var _currentRank:TextField;
		/**
		 *数据 
		 */
		private var _arr:Array;
		/**
		 *页容器 
		 */
		private var _pageContain:MovieClip;
		
		public function RankingPanel()
		{
			super();
//			initUI();
		}
		
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.football.stadium.ranking");
			this.addChild(symbolSkin);
//			setCloseBtn();
			isCloseBtn = true;
			_windowBg = symbolSkin.rankBg;
			isDrag = true;
//			isDrag = true;
//			closeBtn.x = 509 - closeBtn.width ;
			
			_menu = new LadderLvTabMenu();
			_menu.initUI(symbolSkin);
//			closeBtn = symbolSkin.closeBtn;
			_contain = symbolSkin._contain;
			_pageContain = symbolSkin._pageContain;
			_currentRank = symbolSkin._currentRank;
			_viewHeight = symbolSkin.height;
			_viewWidth = symbolSkin.width;
			moveClose(510,5);
//			_page = new PagePanel();
//			_page.callBack = changePage;
//			_pageContain.addChild(_page);
//			closeBtn.addEventListener(MouseEvent.CLICK,oncloseHandler);
			setItem();
			addEvtListener();
			onResizeHandler();
		}
		private function setItem():void
		{
			if(_itemVec != null)return;
			_itemVec = new Vector.<RankingItem>();
			var item:RankingItem;
			for(var i:int = 0;i < ITEMLENGTH;i++)
			{
				item = new RankingItem();
				item.initUI();
				item.y = (item.height + 2)* i;
				_itemVec.push(item);
				_contain.addChild(item);
			}
		}
		/**
		 * 关闭按钮响应
		 * @param e
		 * 
		 */		
//		override protected function onCloseHandler(e :MouseEvent):void
//		{
//			if(this.parent)
//			{
//				this.parent.removeChild(this);
//			}
//			close();
//		}
		
		
		
//		private function oncloseHandler(event:MouseEvent):void
//		{
//			close();
//		}
		/**
		 *移除 
		 * 
		 */
		public function close():void
		{
			if(this.parent)this.parent.removeChild(this);
		}
		private function onMenuHandler(event:Event):void
		{
			show();
		}
		/**
		 *显示 (更新类型)
		 * 
		 */
		public function show():void
		{
			_currentRank.text = "当前天梯等级为"+ levelChange(AppData.getInstance().ladderDataManger.ladderInfovo.ladderlevel) +"级，排名第"+/*levelChange(*/AppData.getInstance().ladderDataManger.ladderInfovo.ladderrank/*)*/+"名" +
								(AppData.getInstance().ladderDataManger.ladderInfovo.ladderrank <100? "" :"，当前排名未进入100名");
			showItem();
		}
		private function levelChange($value:int):String
		{
			switch($value)
			{
				case 1:
					return "D";
				case 2:
					return "C";
				case 3:
					return "B";
				case 4:
					return "A";
				case 5:
					return "S";
			}
			return "D";
		}
		/**
		 *显示 item（换页）
		 * 
		 */
		private function showItem():void
		{
			reset();
			var arr:Array = AppData.getInstance().ladderDataManger.getLadderRankByType(_menu.selectType);
//			arr.sortOn("sort");
			
			var len:int = arr.length > 10? 10:arr.length;
			var i:int = 0;
			for(i;i < len;i++)
			{
				_itemVec[i].setData(arr[i]);
				_contain.addChild(_itemVec[i]);
			}
			
		}
		
		/**
		 * 重置 
		 * 
		 */
		private function reset():void
		{
			for(var i:int = 0;i < ITEMLENGTH;i++)
			{
				_itemVec[i].reSet();
				_contain.addChild(_itemVec[i]);
			}
		}
		/**
		 *换页  
		 * @param $str
		 * 
		 */
		private function changePage($str:String):void
		{
			showItem();
		}
		override public function addEvtListener():void
		{
//			closeBtn.addEventListener(MouseEvent.CLICK,oncloseHandler);
			_menu.addEventListener(Event.CHANGE,onMenuHandler);
		}
		
		override public function removeEvtListener():void
		{
//			closeBtn.removeEventListener(MouseEvent.CLICK,onCloseHandler);
//			_menu.removeEventListener(Event.CHANGE,onMenuHandler);
		}
		
		override public function destroyView():void
		{
			if(this.parent)
				this.parent.removeChild(this);
			removeEvtListener();
			if(_itemVec == null)return;
			while(_itemVec.length)
			{
				_itemVec.pop().destroyView();
			}
			_itemVec = null;
//			closeBtn = null;
			symbolSkin = null;
		}
	}
}